package com.yuandengta.yomall.seckill.controller;

import com.yuandengta.common.utils.R;
import com.yuandengta.yomall.seckill.service.SeckillService;
import com.yuandengta.yomall.seckill.to.SeckillSkuRedisTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 秒杀 {@link SeckillController}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 获取当前时间可以参与的秒杀商品信息
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTO> tos =  seckillService.getCurrentSeckillSkus();

        return R.ok().put("data", tos);
    }

    /**
     * 获取商品秒杀信息
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTO to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().put("data", to);
    }

    /**
     * 秒杀
     * @param killId sessionId_skuId
     * @param key 商品随机码
     * @param num 秒杀数量
     * @return
     */
    @GetMapping("/seckill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        String orderSn = seckillService.seckill(killId, key, num);
        model.addAttribute("orderSn", orderSn);

        return "success";
    }
}
